<style>
.t_judul{
        
        padding:12px;
        font-size:16px;
        font-weight:bold;
        margin-bottom: 10px;
        padding-bottom: 20px;
}

.t_header{
        background:#dddddd;
        vertical-align: middle;
        padding:3px;
        text-align: center;
        border: solid 1px;
        font-weight: bold;
}

.t_isi{
        vertical-align: middle;
        padding:3px;
        border: solid 1px;
        font-weight: bold;
}


	
</style>

<script>
$(document).ready(function() {
    
    $('.d_menu').click(function() {
        var groupid        = $(this).attr('groupid');
        var menuid          = $(this).attr('menuid');
        var nilai      = $(this).is(':checked');
        
        $.post(
            '<?=base_url()?>index.php/grouprole/saveUserRole', 
            {groupid:groupid,menuid:menuid,nilai:nilai},
            function(data) {
                if(data.status == 'OK') {
                    $("#messageBox").removeClass().addClass("errorbox").html(data.pesan).fadeIn(1000).fadeOut(1000);
                }
            },
            "json"
        ).error(function() {
            alert("-");
        });
    });
	
	$('.saveAction').click(function() {
        var groupid        = $(this).attr('groupid');
        var menuid          = $(this).attr('menuid');
		var actid          = $(this).attr('actid');
        var nilai      = $(this).is(':checked');
        
        $.post(
            '<?=base_url()?>index.php/grouprole/saveUserRoleAction', 
            {groupid:groupid,menuid:menuid,nilai:nilai,actid:actid},
            function(data) {
                if(data.status == 'OK') {
                    $("#messageBox").removeClass().addClass("errorbox").html(data.pesan).fadeIn(1000).fadeOut(1000);
                }
            },
            "json"
        ).error(function() {
            alert("-");
        });
    });
    
});

function jump_url(targ,selObj,restore){ //v3.0
	eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
	if (restore) selObj.selectedIndex=0;
}

$(document).ready(function() {
	$("#messageBox").addClass("messagebox");setTimeout(function(){
		$("#messageBox").fadeOut("slow")}, 3000);
	});
function messagebox(){
	$("#messageBox").removeClass().addClass("confirmbox").html("Item has been saved").fadeIn(2000).fadeOut(6000);
}
function alertbox(){
	$("#messageBox").removeClass().addClass("errorbox").html("Oops, there was an error!").fadeIn(2000).fadeOut(6000);
}

</script>

<style type="text/css" media="screen">
<!--
.container {min-width: 170px; margin: 0px auto;position: fixed; right: 0px; top: 0px;}
.messagebox {background-color: #F5F5F5;padding:0px;margin:0px;border: 1px solid #DBDBDB;}
.errorbox {color:#000;background-color:#f96d07;padding:5px;margin:10px 0px;border:1px solid #f27c7c;font-weight:bold;color:white;}
.confirmbox {background-color:#F2FFDB;color:#151515;border:1px solid #9C6;margin:10px 0px;padding:5px;}
-->
</style>
<div class="container">
<div class="messagebox" id="messageBox"></div>
</div>

<div class="box" style="min-height:900px;">
        <!-- box / title -->
        <div class="title">
                <h5>User Role Management</h5>
        </div>
        <!-- end box / title -->
        
        <form id="form" action="<?=$_SERVER['PHP_SELF']?>" method="post">
                <div class="form">
                        <div class="fields">
                                
                                
                                <div class="field">
                                        <div class="label">
                                                <label for="name">User Group:</label>
                                        </div>
                                        <div class="input">
                                                <select name="group" onchange="jump_url('parent',this,0)" >>
                                                <option value="<?= base_url()?>index.php/grouprole/">Choice</option>
                                                <?php
                                                foreach($getUserGroup as $r){
                                                        $SELECTED = $r->user_group_id==$group?"SELECTED":"";
                                                        echo'<option value="'. base_url() .'index.php/grouprole/index/'. $r->user_group_id .'" '. $SELECTED .'>'. $r->user_group_name .'</option>';
                                                }
                                                echo'</select>';
                                                ?>
                                                
                                        </div>
                                </div>
                        </div>
                </div>
        </form><br><br>&nbsp;
        
        <div class="table">
        <?php if($group <> "") { ?>
                
                <table width=100% style="border:solid 1px #aaaaaa;" cellpadding==0 cellspacing==0>
                <thead>
                        <tr height="30px">
                                <th width=30 class="t_header"></th>
                                <th width=30 class="t_header">No</th>
                                <th class="t_header">Page</th>
                                <th width=40% class="t_header">Action</th>
                        </tr>
                </thead>
                <tbody>
                        
                        <?php
                        $i=1;
                        $menus = $this->mmaster->getMenuSistem(0);
			foreach($menus->result() as $r){
                                $CEK = $this->mglobal->showdata("menu_id","t_mtr_user_role",array("user_group_id"=>$group,"menu_id"=>$r->id),"dblokal");
                                $CHECK = $CEK == ""?"":" CHECKED ";
                                echo'<tr height="30px">
                                        <td class="t_isi" align="center"><input type="checkbox" groupid="'.$group.'" menuid="'. $r->id .'" class="d_menu" '. $CHECK .'></td>
                                        <td class="t_isi" align=center>'. $i++ .'</td>
                                        <td class="t_isi">'. $r->name .'</td>
                                        <td class="t_isi"></td>
                                </tr>';
                                $smenu1 = $this->mmaster->getMenuSistem($r->id);
                                if($smenu1->num_rows()>0){
                                        foreach($smenu1->result() as $s){
                                                $CEK = $this->mglobal->showdata("menu_id","t_mtr_user_role",array("user_group_id"=>$group,"menu_id"=>$s->id),"dblokal");
												$CHECK = $CEK == ""?"":" CHECKED ";
												
												$CEK1 = $this->mglobal->showdata("menu_id","t_mtr_user_role",array("user_group_id"=>$group,"menu_id"=>$s->id,"menu_add"=>1),"dblokal");
												$CEK2 = $this->mglobal->showdata("menu_id","t_mtr_user_role",array("user_group_id"=>$group,"menu_id"=>$s->id,"menu_view"=>1),"dblokal");
												$CEK3 = $this->mglobal->showdata("menu_id","t_mtr_user_role",array("user_group_id"=>$group,"menu_id"=>$s->id,"menu_edit"=>1),"dblokal");
												$CEK4 = $this->mglobal->showdata("menu_id","t_mtr_user_role",array("user_group_id"=>$group,"menu_id"=>$s->id,"menu_delete"=>1),"dblokal");
												$CEK5 = $this->mglobal->showdata("menu_id","t_mtr_user_role",array("user_group_id"=>$group,"menu_id"=>$s->id,"menu_approve"=>1),"dblokal");
												$CEK6 = $this->mglobal->showdata("menu_id","t_mtr_user_role",array("user_group_id"=>$group,"menu_id"=>$s->id,"menu_reject"=>1),"dblokal");
												
												$CHECK1 = $CEK1 == ""?"":" CHECKED ";
												$CHECK2 = $CEK2 == ""?"":" CHECKED ";
												$CHECK3 = $CEK3 == ""?"":" CHECKED ";
												$CHECK4 = $CEK4 == ""?"":" CHECKED ";
												$CHECK5 = $CEK5 == ""?"":" CHECKED ";
												$CHECK6 = $CEK6 == ""?"":" CHECKED ";
												
                                                echo'<tr height="30px">
                                                        <td class="t_isi" align="center"><input type="checkbox" groupid="'.$group.'" menuid="'. $s->id .'" class="d_menu" '. $CHECK .'></td>
                                                        <td class="t_isi" align=center>'. $i++ .'</td>
                                                        <td class="t_isi">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'. $s->name .'</td>
                                                        <td class="t_isi"id="formats'.$i.'" align=center>
																<input type="checkbox" id="check1'.$i.'" class="saveAction" groupid="'.$group.'" menuid="'. $s->id .'" actid="menu_add" '.$CHECK1.' /><label for="check1'.$i.'">Add</label>
																<input type="checkbox" id="check2'.$i.'" class="saveAction" groupid="'.$group.'" menuid="'. $s->id .'" actid="menu_view" '.$CHECK2.' /><label for="check2'.$i.'">View</label>
																<input type="checkbox" id="check3'.$i.'" class="saveAction" groupid="'.$group.'" menuid="'. $s->id .'" actid="menu_edit" '.$CHECK3.' /><label for="check3'.$i.'">Edit</label>
																<input type="checkbox" id="check4'.$i.'" class="saveAction" groupid="'.$group.'" menuid="'. $s->id .'" actid="menu_delete" '.$CHECK4.' /><label for="check4'.$i.'">Delete</label>
																<input type="checkbox" id="check5'.$i.'" class="saveAction" groupid="'.$group.'" menuid="'. $s->id .'" actid="menu_approve" '.$CHECK5.' /><label for="check5'.$i.'">Approve</label>
																<input type="checkbox" id="check6'.$i.'" class="saveAction" groupid="'.$group.'" menuid="'. $s->id .'" actid="menu_reject" '.$CHECK6.' /><label for="check6'.$i.'">Reject</label>
														</td>
                                                </tr>';
                                                $smenu2 = $this->mmaster->getMenuSistem($s->id);
                                                if($smenu2->num_rows()>0){
                                                        foreach($smenu2->result() as $t){
                                                                $CEK = $this->mglobal->showdata("menu_id","t_mtr_user_role",array("user_group_id"=>$group,"menu_id"=>$t->id),"dblokal");
                                                                $CHECK = $CEK == ""?"":" CHECKED ";
																
																$CEK1 = $this->mglobal->showdata("menu_id","t_mtr_user_role",array("user_group_id"=>$group,"menu_id"=>$t->id,"menu_add"=>1),"dblokal");
																$CEK2 = $this->mglobal->showdata("menu_id","t_mtr_user_role",array("user_group_id"=>$group,"menu_id"=>$t->id,"menu_view"=>1),"dblokal");
																$CEK3 = $this->mglobal->showdata("menu_id","t_mtr_user_role",array("user_group_id"=>$group,"menu_id"=>$t->id,"menu_edit"=>1),"dblokal");
																$CEK4 = $this->mglobal->showdata("menu_id","t_mtr_user_role",array("user_group_id"=>$group,"menu_id"=>$t->id,"menu_delete"=>1),"dblokal");
																$CEK5 = $this->mglobal->showdata("menu_id","t_mtr_user_role",array("user_group_id"=>$group,"menu_id"=>$t->id,"menu_approve"=>1),"dblokal");
																$CEK6 = $this->mglobal->showdata("menu_id","t_mtr_user_role",array("user_group_id"=>$group,"menu_id"=>$t->id,"menu_reject"=>1),"dblokal");
																
																$CHECK1 = $CEK1 == ""?"":" CHECKED ";
																$CHECK2 = $CEK2 == ""?"":" CHECKED ";
																$CHECK3 = $CEK3 == ""?"":" CHECKED ";
																$CHECK4 = $CEK4 == ""?"":" CHECKED ";
																$CHECK5 = $CEK5 == ""?"":" CHECKED ";
																$CHECK6 = $CEK6 == ""?"":" CHECKED ";
																
                                                                echo'<tr height="30px">
                                                                        <td class="t_isi" align="center"><input type="checkbox" groupid="'.$group.'" menuid="'. $t->id .'" class="d_menu" '. $CHECK .'></td>
                                                                        <td class="t_isi" align=center>'. $i++ .'</td>
                                                                        <td class="t_isi">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'. $t->name .'</td>
                                                                        <td class="t_isi"id="formats'.$i.'" align=center>
																				<input type="checkbox" id="check1'.$i.'" class="saveAction" groupid="'.$group.'" menuid="'. $t->id .'" actid="menu_add" '.$CHECK1.' /><label for="check1'.$i.'">Add</label>
																				<input type="checkbox" id="check2'.$i.'" class="saveAction" groupid="'.$group.'" menuid="'. $t->id .'" actid="menu_view" '.$CHECK2.' /><label for="check2'.$i.'">View</label>
																				<input type="checkbox" id="check3'.$i.'" class="saveAction" groupid="'.$group.'" menuid="'. $t->id .'" actid="menu_edit" '.$CHECK3.' /><label for="check3'.$i.'">Edit</label>
																				<input type="checkbox" id="check4'.$i.'" class="saveAction" groupid="'.$group.'" menuid="'. $t->id .'" actid="menu_delete" '.$CHECK4.' /><label for="check4'.$i.'">Delete</label>
																				<input type="checkbox" id="check5'.$i.'" class="saveAction" groupid="'.$group.'" menuid="'. $t->id .'" actid="menu_approve" '.$CHECK5.' /><label for="check5'.$i.'">Approve</label>
																				<input type="checkbox" id="check6'.$i.'" class="saveAction" groupid="'.$group.'" menuid="'. $t->id .'" actid="menu_reject" '.$CHECK6.' /><label for="check6'.$i.'">Reject</label>
																		</td>
                                                                </tr>';
                                                        }
                                                }
                                        }
                                }
                        }
                        ?>
                </tbody>
                </table>
        <?php } ?>
        </div>
</div>
<script>
		$(function() {
		<?php
		for($k=1;$k<=$i;$k++){
			echo'$( "#formats'.$k.'" ).buttonset();';
		}
		?>
	});
</script>